package com.qf.openapi.web.master.controller;


import com.qf.openapi.web.master.entity.Api;
import com.qf.openapi.web.master.service.ApiService;
import com.qf.openapi.web.master.utils.Constants;
import com.qf.openapi.web.master.vo.ResultVo;
import com.qf.openapi.web.master.vo.TableVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/sys/api_mapping")
@Slf4j
public class ApiController {
    
    @Autowired
    private ApiService apiService;
    
    @GetMapping("/table")
    public TableVo<Api> getApiByCondition(Integer page, Integer limit, Api api) {
        log.debug("收到的参数：page={}, limit={}, api={}", page, limit, api);
        
        return apiService.getApiByCondition(page, limit, api);
    }
    
    
    @PostMapping("/add")
    public ResultVo add(Api api) {
        log.info("前端收参到后端为：api={}", api);
        
        ResultVo resultVo = apiService.addApi(api);
        
        log.info("后端插入数据库的resultVo为：resultVo={}", resultVo);
        
        return resultVo;
    }
    
    
    @PostMapping("/update")
    public ResultVo update(Api api) {
        log.debug("前端收参到后端为：api={}", api);
        
        
        ResultVo resultVo = apiService.updateApi(api);
        
        log.info("后端返回给前端的resultVo为：resultVo={}", resultVo);
        
        return resultVo;
    }
    
    
    @PostMapping("/del")
    public ResultVo del(Integer[] ids) {
        log.debug("ids={}", Arrays.toString(ids));
        
        ResultVo resultVo = apiService.delApi(ids);
        
        log.info("后端返回给前端的resultVo为：resultVo={}", resultVo);
        
        return resultVo;
    }
    
    
    @PostMapping(value = "/upload")
    public ResultVo upload(MultipartFile file) throws IOException {
        
        return apiService.addApis(file);
        
    }
}
